package com.example.tests;

public class GroupData implements Comparable<GroupData> {
    private String groupName;
    private String groupHeader;
    private String groupFooter;
    private String id;

    private boolean isNull(String string){
        return (string == null) || string.equals("null");
    }

    public GroupData(String groupName, String groupHeader, String groupFooter) {
        this.groupName = groupName;
        this.groupFooter = groupFooter;
        this.groupHeader = groupHeader;
    }
    public GroupData(){
        this("", "", "");
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public void setGroupHeader(String groupHeader) {
        this.groupHeader = groupHeader;
    }

    public void setGroupFooter(String groupFooter) {
        this.groupFooter = groupFooter;
    }

    public String getId() {
        return id;
    }

    public String getGroupName() {
        return groupName;
    }

    public String getGroupHeader() {
        return groupHeader;
    }

    public String getGroupFooter() {
        return groupFooter;
    }

    public GroupData storeUnChangedFields(GroupData that){
        GroupData result = new GroupData();

        if (isNull(this.getGroupHeader()))
            result.setGroupHeader(that.getGroupHeader());
        else
            result.setGroupHeader(this.getGroupHeader());

        if (isNull(this.getGroupFooter()))
            result.setGroupFooter(that.getGroupFooter());
        else
            result.setGroupFooter(this.getGroupFooter());

        if (isNull(this.getGroupName()))
            result.setGroupName(that.getGroupName());
        else
            result.setGroupName(this.getGroupName());

        return result;
    }

    @Override
    public String toString() {
        return "GroupData{" +
                "groupName='" + groupName + '\'' +
                ", groupHeader='" + groupHeader + '\'' +
                ", groupFooter='" + groupFooter + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        GroupData groupData = (GroupData) o;

        String thisName = this.getGroupName() == null ? "" : this.getGroupName();
        String otherName = groupData.getGroupName() == null ? "" : groupData.getGroupName();

        if (!thisName.equals(otherName)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result = groupName != null ? groupName.hashCode() : 0;
        result = 31 * result + (groupHeader != null ? groupHeader.hashCode() : 0);
        result = 31 * result + (groupFooter != null ? groupFooter.hashCode() : 0);
        return result;
    }

    @Override
    public int compareTo(GroupData other) {
        String thisName = this.getGroupName() == null ? "" : this.getGroupName();
        String otherName = other.getGroupName() == null ? "" : other.getGroupName();
        return thisName.toLowerCase().compareTo(otherName.toLowerCase());
    }

    public GroupData setName(String groupName) {
        setGroupName(groupName);
        return this;
    }

    public GroupData setHeader(String groupHeader) {
        setGroupHeader(groupHeader);
        return this;
    }

    public GroupData setFooter(String groupFooter) {
        setGroupFooter(groupFooter);
        return this;
    }

    public GroupData setId(String id) {
        this.id = id;
        return this;
    }
}
